<?php
namespace Portal\MainBundle\Utils;

class customFunctions
{
  public static function filterForLogin($dirtyText)
  {
    $clearText = $dirtyText;
    return $clearText;
  }
  
  public static function clear($dirtyText)
  {
    $clearText = $dirtyText;
    return $clearText;
  }
  
  public static function clearForUrl($dirtyText)
  {
    $clearText = $dirtyText;
    return $clearText;
  }
  
  public static function pagination($page, $per_page, $total, $pageName = 'page', $delta = 0)
  {
    $link = $_SERVER['REQUEST_URI'];
   
    $queryString = $_SERVER['QUERY_STRING'];
    
    
    if (strlen($queryString)>1) {      
      // xervee URL dotor page gesen ug baival tuxain page iin urd ni "?" esvel "&" a
      $pos = strpos($queryString, '&' . $pageName);
      
      if($pos === false)
      { 
        $operator = '&';        
      }else{
        $operator = '&';
      }
    } else {
      $operator = '?';
    }
    $str_text = $operator.$pageName.'=' . $page;
    
    $new_link = str_replace($str_text, '', $link);
    
    
    if ($delta == 0) {
      $delta = 2;
    }
    if ($total % $per_page == 0) {
      $total_page = $total / $per_page;
    } else {
      $total_page = floor($total / $per_page) + 1;
    }
    $page_html = 'Хуудас: ';
    if ($page > $delta) {
      $start_page = $page - $delta;
    } else {
      $start_page = 1;
    }
    if (($page > $delta) && ($page < $total_page - $delta)) {
      $end_page = $page + $delta;
    } elseif (($page < $delta + 1) && ($page < $total_page - $delta)) {
      $end_page = $page + $delta;
    } elseif (($page < $delta + 1) && ($total_page < $delta + 1)) {
      $end_page = $total_page;
    } else {
      $end_page = $total_page;
    }
    if ($page > 1) {
      $page_html .= '<a href="' . $new_link .
              $operator . $pageName.'=1" class="paginate" title="Эхний"><<</a> ';
      $page_html .= '<a href="' . $new_link . $operator . $pageName. '=' . ($page - 1) .
              '" class="paginate" title="Өмнөх"><</a> ';
    }
    for ($i = $start_page; $i <= $end_page; $i++) {
      if ($i != $page) {
        $page_html .= ' <a href="' . $new_link . $operator . $pageName . '=' . $i .
                '" class="paginate">' . $i . '</a> ';
      } else {
        $page_html .= ' <a href="' . $new_link . $operator . $pageName . '=' . $i .
                '" class="selected_page" style="font-weight: bold;color:red">' . $i . '</a> ';
      }
    }
    if ($page < $total_page) {
      $page_html .= '<a href="' . $new_link . $operator . $pageName. '=' . ($page + 1) .
              '" class="paginate" title="Дараагийн">></a> ';
      $page_html .= '<a href="' . $new_link . $operator . $pageName . '=' . $total_page .
              '" class="paginate" title="Сүүлчийн">>></a> ';
    }
    $min = ($page - 1) * $per_page + 1;
    if ($page < $total_page) {
      $max = $min + $per_page - 1;
    } else {
      $max = $total;
    }
    $page_html .= ' Нийт <b>' . $total . '</b> үр дүн байна. Энэ хуудсанд ' . $min .
            '-' . $max;
    return $page_html;
  }
  
  public static function randomRassword($length = 8)
  {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
    $password = substr(str_shuffle($chars), 0, $length);
    
    return $password;
  }
  
  public static function getRoleStringFromRoleArray($role)
  {
    $roleString = '';
    if(!is_array($role))
    {
      return $roleString;
    }else{
      foreach ($role AS $key => $value) {
        if ($key == count($role) - 1) {
          $roleString .= $value;
        } else {
          $roleString .= $value . ', ';
        }
      }
    }
    
    return $roleString;
  }
  
  public static function getDaysBetweenDate($date1, $date2)
  {
    $interval = $date1->diff($date2);
    return $interval->days;
  }
  
  public static function generateInvoiceNumber($integer)
  {
    $integer = (int) $integer;
    return (string) str_pad($integer, 5, "0", STR_PAD_LEFT);
  }
}


?>
